package com.lzm.ydpt.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.byt.framlib.commonutils.DisplayUtil;
import com.byt.framlib.commonutils.KeyBordUtil;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.R;
import com.lzm.ydpt.view.CEditText;

import java.util.Timer;
import java.util.TimerTask;

public class PwdDialog implements View.OnClickListener {

    private Dialog mDialog;
    private View dialogView;

    private TextView tv_show_content;
    private TextView tv_submit_data;
    private ImageView img_cal_dialog;
    private CEditText c_edit_text_view_circle;


    private Builder mBuilder;
    private String pwdMsg;

    public PwdDialog(Builder builder) {
        this.mBuilder = builder;
        mDialog = new Dialog(mBuilder.getContext(), R.style.MyDialogStyle);
        dialogView = View.inflate(mBuilder.getContext(), R.layout.dialog_pwd_layout, null);
        tv_show_content = dialogView.findViewById(R.id.tv_show_content);
        tv_submit_data = dialogView.findViewById(R.id.tv_submit_data);
        c_edit_text_view_circle = dialogView.findViewById(R.id.c_edit_text_view_circle);

        mDialog.setContentView(dialogView);
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DisplayUtil.getScreenWidth(mBuilder.getContext());
        lp.height = DisplayUtil.getScreenHeight(mBuilder.getContext());
        dialogWindow.setAttributes(lp);
        mDialog.setCanceledOnTouchOutside(builder.isTouchOutside());
        tv_show_content.setText(builder.getTitle());
        img_cal_dialog.setOnClickListener(this);
        tv_submit_data.setOnClickListener(this);
        c_edit_text_view_circle.setOnFinishListener(new CEditText.OnFinishListener() {
            @Override
            public void onFinish(String msg) {
                pwdMsg = msg;
            }
        });
        c_edit_text_view_circle.setFocusable(true);
        c_edit_text_view_circle.setFocusableInTouchMode(true);
        c_edit_text_view_circle.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               KeyBordUtil.showSoftKeyboard(c_edit_text_view_circle);
                           }
                       },
                200);
    }

    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_submit_data:
                if (mBuilder.getPwdDialogCall() != null && pwdMsg.length() == 4) {
                    mBuilder.getPwdDialogCall().pwdCallBack(pwdMsg);
                }
                dismiss();
                break;
        }
    }

    public static class Builder {

        private boolean isTouchOutside;
        private Context mContext;
        private String title;
        private boolean isReset;
        private PwdDialogCall pwdDialogCall;

        public Builder(Context context) {
            this.mContext = context;
            this.isTouchOutside = true;
            this.title = "设置密码";
        }

        public boolean isTouchOutside() {
            return isTouchOutside;
        }

        public Builder setCanceledOnTouchOutside(boolean isTouchOutside) {
            this.isTouchOutside = isTouchOutside;
            return this;
        }

        public PwdDialogCall getPwdDialogCall() {
            return pwdDialogCall;
        }

        public Builder setPwdDialogCall(PwdDialogCall pwdDialogCall) {
            this.pwdDialogCall = pwdDialogCall;
            return this;
        }

        public PwdDialog build() {
            return new PwdDialog(this);
        }

        public String getTitle() {
            return title;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public boolean isReset() {
            return isReset;
        }

        public Builder setReset(boolean reset) {
            isReset = reset;
            return this;
        }

        public Context getContext() {
            return mContext;
        }

        public void setContext(Context context) {
            mContext = context;
        }

        public void setTouchOutside(boolean touchOutside) {
            isTouchOutside = touchOutside;
        }
    }

    public interface PwdDialogCall {
        void pwdCallBack(String pwd);

        void resetClick();
    }
}
